package com.king.headline.UI.webview;

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.king.headline.R;
import com.king.headline.base.BaseActivity;

import org.json.JSONObject;

/**
 * JS回调anroid与java的处理类
 *
 * @author Administrator
 */
public class WebViewActivity extends BaseActivity {
    private ImageView iv_back;// 返回键
    private TextView tv_title;// 标题
    private String title;
    private static ProgressDialog pDialog;
    private String url;
    private WebView webView;
    private ProgressBar myProgressBar;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        url = getIntent().getStringExtra("url");
        if (!TextUtils.isEmpty(url) && !url.contains("http")) {
            url = "http://" + url;
        }
        title = getIntent().getStringExtra("title");
        baseSetContentView(R.layout.activity_jspwebview);
        initView();
        setTopBarAndAction();
        String ua = webView.getSettings().getUserAgentString();
        webView.getSettings().setUserAgentString(ua + "; HFWSH /1.0");

    }
    private void setTopBarAndAction() {
        setBarTitleText(title);
        setBackIcon(true);
    }

    @SuppressLint({"SetJavaScriptEnabled", "NewApi"})
    private void initView() {
        myProgressBar = (ProgressBar) findViewById(R.id.myProgressBar);
        webView = (WebView) findViewById(R.id.webview);
        WebSettings webSettings = webView.getSettings();
        //支持javascript
        webSettings.setJavaScriptEnabled(true);
        // 设置可以支持缩放
        webSettings.setSupportZoom(true);
//		// 设置出现缩放工具 
        webSettings.setBuiltInZoomControls(true);
        if (android.os.Build.VERSION.SDK_INT >= 11) {
            webSettings.setDisplayZoomControls(false);
        }
        //扩大比例的缩放
        webSettings.setUseWideViewPort(true);
        //自适应屏幕(和部分h5效果冲突,暂时注释掉)
        webView.addJavascriptInterface(this, "Android");
        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (newProgress == 100) {
                    myProgressBar.setVisibility(View.GONE);
                } else {
                    if (View.GONE == myProgressBar.getVisibility()) {
                        myProgressBar.setVisibility(View.VISIBLE);
                    }
                    myProgressBar.setProgress(newProgress);
                }
                super.onProgressChanged(view, newProgress);
            }

            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
                if (TextUtils.isEmpty(WebViewActivity.this.title)) {
                    tv_title.setText(title);
                }
            }

        });
        webView.setWebViewClient(new InsideWebViewClient(
                WebViewActivity.this));
        webView.loadUrl(url);


    }

    String clubid;
    String teedate;
    private JSONObject obj;


    /**
     * 按键响应，在WebView中查看网页时，按返回键的时候按浏览历史退回,如果不做此项处理则整个WebView返回退出
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // Check if the key event was the Back button and if there's history
        if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
            // 返回键退回
            webView.goBack();
            return true;
        } else if ((keyCode == KeyEvent.KEYCODE_BACK)) {
            finish();
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (pDialog != null) {
            pDialog.dismiss();
            pDialog = null;
        }

        WebViewActivity.this.deleteDatabase("webview.db");
        WebViewActivity.this.deleteDatabase("webviewCache.db");
    }




}
